﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XGameLibrary.Controls
{
    public class ControlManager : List<Control>
    {
        #region Fields and Properties

        Control selectedControl;
        bool acceptInput = false;

        static SpriteFont spriteFont;

        public static SpriteFont SpriteFont
        {
            get { return spriteFont; }
        }

        public bool AcceptInput
        {
            get { return acceptInput; }
            set { acceptInput = value; }
        }

        #endregion

        #region Event Region

        public event EventHandler FocusChanged;

        #endregion

        #region Constructors

        public ControlManager(SpriteFont spriteFont)
            : base()
        {
            ControlManager.spriteFont = spriteFont;
        }

        public ControlManager(SpriteFont spriteFont, int capacity)
            : base(capacity)
        {
            ControlManager.spriteFont = spriteFont;
        }

        public ControlManager(SpriteFont spriteFont, IEnumerable<Control> collection) :
            base(collection)
        {
            ControlManager.spriteFont = spriteFont;
        }

        #endregion

        #region Methods

        public bool Update(GameTime gameTime)
        {
            if (Count == 0)
                return false;

            MouseState state = Mouse.GetState();

            foreach (Control c in this)
            {
                if (c.Enabled)
                {
                    c.Update(gameTime);
                    if (state.X >= c.Position.X &&
                        state.X <= c.Position.X + c.Size.X &&
                        state.Y >= c.Position.Y &&
                        state.Y <= c.Position.Y + c.Size.Y)
                    {
                        SelectControl(c);
                        c.HandleMouse();
                        return true;
                    }

                }
            }

            foreach (Control c in this)
            {
                if (c.HasFocus)
                {
                    c.HandleInput();
                    break;
                }
            }

            //if (!AcceptInput)
                return false;

            //if (InputHandler.KeyPressed(Keys.Up))
            //    PreviousControl();

            //if (InputHandler.KeyPressed(Keys.Down))
            //    NextControl();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Control c in this)
            {
                if (c.Visible)
                    c.Draw(spriteBatch);
            }
        }

        public void SelectControl(Control c)
        {
            if (selectedControl != null)
                selectedControl.HasFocus = false;
            selectedControl = c;
            if (FocusChanged != null)
                FocusChanged(selectedControl, null);
            selectedControl.HasFocus = true;
        }

        //public void NextControl()
        //{
        //    if (Count == 0)
        //        return;

        //    int currentControl = selectedControl;

        //    this[selectedControl].HasFocus = false;

        //    do
        //    {
        //        selectedControl++;

        //        if (selectedControl == Count)
        //            selectedControl = 0;

        //        if (this[selectedControl].TabStop && this[selectedControl].Enabled)
        //        {
        //            if (FocusChanged != null)
        //                FocusChanged(this[selectedControl], null);

        //            break;
        //        }

        //    } while (currentControl != selectedControl);

        //    this[selectedControl].HasFocus = true;
        //}

        //public void PreviousControl()
        //{
        //    if (Count == 0)
        //        return;

        //    int currentControl = selectedControl;

        //    this[selectedControl].HasFocus = false;

        //    do
        //    {
        //        selectedControl--;

        //        if (selectedControl < 0)
        //            selectedControl = Count - 1;

        //        if (this[selectedControl].TabStop && this[selectedControl].Enabled)
        //        {
        //            if (FocusChanged != null)
        //                FocusChanged(this[selectedControl], null);

        //            break;
        //        }
        //    } while (currentControl != selectedControl);

        //    this[selectedControl].HasFocus = true;
        //}

        #endregion
    }
}
